package org.example.sword2offer.primary;

import java.util.Arrays;

/**
 * @author: lynn
 * @Descript: 扑克牌顺子
 *
 * @date: 2021/3/21 22:51
 * @version: 1.0
 */
public class Q45_PokerContinuous {
    public static void main(String[] args) {
        int[] numbers={2,4,3,6,0};
        System.out.println(Boolean.toString(IsContinuous(numbers)));
    }
    //题目简化了,只判断是不是5顺
    public static boolean IsContinuous(int [] numbers) {
        if (numbers.length==0||numbers.length!=5){
            return false;
        }
        //数组排序
        Arrays.sort(numbers);
        int zeroCount = 0, slots = 0;

        /*for(int i = 0 ; i < numbers.length ; i++){
            //如果遇到两张相同的非王牌则不可能组成顺子，这点很容易忽略！！！
            if(i > 0 && numbers[i - 1] != 0){
                if(numbers[i] == numbers[i - 1]){
                    return false;
                }else{
                    slots += numbers[i] - numbers[i - 1] - 1;
                }

            }
            zeroCount = (numbers[i] == 0) ? ++zeroCount : zeroCount;
        }*/
        for (int i = 0; i <numbers.length ; i++) {
            if (i>0 && numbers[i-1]!=0){
                if (numbers[i]==numbers[i-1]){
                    return false;
                }else {
                    //注意点
                    slots+=numbers[i]-numbers[i-1]-1;
                }

            }
            zeroCount=(numbers[i]==0)?++zeroCount:zeroCount;

        }


        return zeroCount >= slots;
    }
}
